//
//  CertificationViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/5/23.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "CertificationViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "BindingViewController.h"
#import "UserProtocolViewController.h"
#import "YoungAllData.h"
#import "FinishWithDrawViewController.h"
#import "GMDCircleLoader.h"

@interface CertificationViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate,YoungDelegate>
{
    UITextField *zTextField;
    UITextField *zPersonField;
    UITextField *zSetPassword;
    UITextField *zSurePassword;
    UIPickerView *_picker;
    UIButton *zPickerBtn;
}
@property(nonatomic,assign)NSInteger zIndex;
@property(nonatomic,retain)NSMutableArray *zArrayData;
@end

@implementation CertificationViewController
@synthesize zIndex,zArrayData;
-(void)viewWillAppear:(BOOL)animated{
    
}
#pragma mark - 获取实名认证回调
-(void)getID5:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    NSString *isBank = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"IsBindBankcard"]];
    if ([[NSString stringWithFormat:@"%@",[sign objectForKey:@"status"]]isEqualToString:@"success"]) {
        if ([isBank isEqualToString:@"1"]) {
            FinishWithDrawViewController *finish = [[FinishWithDrawViewController alloc]init];
            finish.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:finish animated:YES];
        }else{
            BindingViewController *finish = [[BindingViewController alloc]init];
            //finish.hidesBottomBarWhenPushed = YES;
            finish.isWhere = @"1";
            finish.zName = [NSString stringWithFormat:@"%@",[[sign objectForKey:@"data"] objectForKey:@"RealName"]];
            finish.zZhengType = @"身份证号";
            finish.zNumber = [NSString stringWithFormat:@"%@",[[sign objectForKey:@"data"] objectForKey:@"CertificateNumber"]];
            [self.navigationController pushViewController:finish animated:NO];
        }
    }else{
        [self initUI];
        UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
        [singleTapGestureRecognizer setNumberOfTapsRequired:1];
        [self.view addGestureRecognizer:singleTapGestureRecognizer];
        
        zArrayData = [[NSMutableArray alloc]initWithObjects:@"身份证号", nil];
    }
}
#pragma mark - 回调失败
-(void)requistFail{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBarHidden=YES;

    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"实名认证";
    
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate =self;
    [data getID5:[[NSUserDefaults standardUserDefaults] objectForKey:@"userID"]];
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
}
#pragma mark - 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - singleTap
-(void)singleTap{
    _picker.hidden = YES;
    zPickerBtn.hidden = YES;
    [zTextField resignFirstResponder];
    [zPersonField resignFirstResponder];
    [zSetPassword resignFirstResponder];
    [zSurePassword resignFirstResponder];
    self.tabBarController.tabBar.hidden = NO;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}

//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([textField isEqual:zSetPassword] || [textField isEqual:zSurePassword]) {
        CGRect frame = textField.frame;
        int offset = frame.origin.y + 32 - (self.view.frame.size.height - 350);//键盘高度216
        
        NSTimeInterval animationDuration = 0.30f;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        
        [UIView commitAnimations];
    }

}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([textField isEqual:zSetPassword] || [textField isEqual:zSurePassword]) {
        self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        [textField resignFirstResponder];
    }

}

#pragma mark - initUI
-(void)initUI{
//    UIButton *belowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    belowBtn.frame = CGRectMake(0,SCREEN_HEIGHT-(0.15625*SCREEN_WIDTH) , SCREEN_WIDTH, 0.15625*SCREEN_WIDTH);
//    [belowBtn setBackgroundImage:[UIImage imageNamed:@"信息_06"] forState:UIControlStateNormal];
//    [belowBtn addTarget:self action:@selector(backGo) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:belowBtn];
    
    UILabel *certification = [[UILabel alloc]initWithFrame:CGRectMake(10, navView.bottom+20, 120, 20)];
    certification.text = @"实名认证";
    certification.textColor  = [UIColor blackColor];
    certification.textAlignment = NSTextAlignmentLeft;
    certification.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:certification];
    
    zTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, certification.bottom+10 ,SCREEN_WIDTH-40 , 30)];
    zTextField.tag = 1;
    zTextField.placeholder = @"姓名";
    zTextField.font = [UIFont systemFontOfSize:14];
    zTextField.backgroundColor= [UIColor whiteColor];
    [self.view addSubview:zTextField];
    zTextField.delegate = self;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20, zTextField.bottom+5, SCREEN_WIDTH-40, 30);
    //[btn setBackgroundImage:[UIImage imageNamed:@"下拉"] forState:UIControlStateNormal];
    [btn setTitle:@"证件类型" forState:UIControlStateNormal];
    [btn setTitleColor:[self colorWithHexString:@"#CDCDD2"] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    btn.backgroundColor = [UIColor whiteColor];
    btn.tag = 900;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btn addTarget:self action:@selector(clickZheng:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(SCREEN_WIDTH-40-25, 0, 25, 30);
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"下拉"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(clickZheng:) forControlEvents:UIControlEventTouchUpInside];
    [btn addSubview:rightBtn];
    
    zPersonField = [[UITextField alloc] initWithFrame:CGRectMake(20, btn.bottom+5 ,SCREEN_WIDTH-40 , 30)];
    zPersonField.tag = 2;
    zPersonField.placeholder = @"身份证号码";
    zPersonField.font = [UIFont systemFontOfSize:14];
    zPersonField.backgroundColor= [UIColor whiteColor];
    [self.view addSubview:zPersonField];
    zPersonField.delegate = self;

    UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-58-130, zPersonField.bottom, 120, 20)];
    
    leftLabel.text = @"点击绑定,即视为同意";
    leftLabel.textColor  = [self colorWithHexString:@"#A6A6A6"];
    leftLabel.textAlignment = NSTextAlignmentRight;
    leftLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:leftLabel];
    
    UIButton *userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    userBtn.frame = CGRectMake(leftLabel.right, leftLabel.top, 48, 20);
    [userBtn setTitle:@"用户协议" forState:UIControlStateNormal];
    [userBtn setTitleColor:[self colorWithHexString:@"#67A8DB"] forState:UIControlStateNormal];
    userBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [userBtn addTarget:self action:@selector(clickUserBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:userBtn];

    
    UILabel *setPassword = [[UILabel alloc]initWithFrame:CGRectMake(10, userBtn.bottom+20, 120, 20)];
    setPassword.text = @"设置支付密码";
    setPassword.textColor  = [UIColor blackColor];
    setPassword.textAlignment = NSTextAlignmentLeft;
    setPassword.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:setPassword];
    
    
    zSetPassword = [[UITextField alloc] initWithFrame:CGRectMake(20, setPassword.bottom+20 ,SCREEN_WIDTH-40 , 30)];
    zSetPassword.tag = 4;
    zSetPassword.placeholder = @"支付密码";
    zSetPassword.font = [UIFont systemFontOfSize:14];
    zSetPassword.keyboardType = UIKeyboardTypeNumberPad;
    zSetPassword.backgroundColor= [UIColor whiteColor];
    [self.view addSubview:zSetPassword];
    zSetPassword.delegate = self;

    
    zSurePassword = [[UITextField alloc] initWithFrame:CGRectMake(20, zSetPassword.bottom+5 ,SCREEN_WIDTH-40 , 30)];
    zSurePassword.tag = 5;
    zSurePassword.placeholder = @"确认支付密码";
    zSurePassword.keyboardType = UIKeyboardTypeNumberPad;
    zSurePassword.font = [UIFont systemFontOfSize:14];
    zSurePassword.backgroundColor= [UIColor whiteColor];
    [self.view addSubview:zSurePassword];
    zSurePassword.delegate = self;

    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nextBtn.frame = CGRectMake(20, zSurePassword.bottom+40, SCREEN_WIDTH-40, 40);
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"shimingrenzheng"] forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(clickNextBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
}

#pragma mark - 下一步
-(void)clickNextBtn:(UIButton*)sender{
    //先将未到时间执行前的任务取消。
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething1:) object:sender];
    [self performSelector:@selector(todoSomething1:) withObject:sender afterDelay:0.2f];
}
-(void)todoSomething1:(id)sender{
    if (zTextField.text.length <= 0 ||zTextField.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入姓名" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (zPersonField.text.length <= 0 ||zPersonField.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入身份号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (zSetPassword.text.length < 6) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"支付密码最少6位" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (zSurePassword.text.length < 6) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"支付密码最少6位" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }if (![zSurePassword.text isEqualToString:zSetPassword.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"确认支付密码不正确" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    [data shiAll:[[NSUserDefaults standardUserDefaults] objectForKey:@"userID"] andRealName:zTextField.text andCertificateNumber:zPersonField.text andTradePass:zSetPassword.text andReTradePass:zSurePassword.text];
}
#pragma mark - 设置密码实名认证回调
-(void)shiAll:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    BindingViewController *binding = [[BindingViewController alloc]init];
    binding.zName = zTextField.text;
    binding.zZhengType = @"身份证号";
    binding.zNumber = zPersonField.text;
    binding.isWhere = @"2";
    [self.navigationController pushViewController:binding animated:YES];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - 用户协议
-(void)clickUserBtn{
    UserProtocolViewController *userProtocol = [[UserProtocolViewController alloc]init];
    [self.navigationController pushViewController:userProtocol animated:YES];
}
#pragma mark - 证件类型
-(void)clickZheng:(UIButton *)sender{
    self.tabBarController.tabBar.hidden = YES;
    [zTextField resignFirstResponder];
    [zPersonField resignFirstResponder];
    [zSetPassword resignFirstResponder];
    [zSurePassword resignFirstResponder];

    //先将未到时间执行前的任务取消。
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething:) object:sender];
    [self performSelector:@selector(todoSomething:) withObject:sender afterDelay:0.2f];
    
}
- (void)todoSomething:(id)sender{
    zIndex = 0;
    if (_picker) {
        _picker.hidden = NO;
        [_picker reloadAllComponents];
    }else{
    //创建省市区选择框
    _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 180, SCREEN_WIDTH, 200)];
    _picker.backgroundColor = LineColor;
    _picker.layer.borderColor = BackColor.CGColor;
    _picker.layer.borderWidth = 1.0;
    _picker.dataSource = self;
    _picker.delegate = self;
    _picker.hidden = NO;
    _picker.showsSelectionIndicator = NO;
    [self.view addSubview:_picker];
    }
    if (zPickerBtn) {
        zPickerBtn.hidden = NO;
    }else{
        zPickerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [zPickerBtn setBackgroundColor:[UIColor blackColor]];
        zPickerBtn.alpha = 0.4;
        zPickerBtn.frame = CGRectMake(0, SCREEN_HEIGHT - 103, SCREEN_WIDTH, 26);
        [zPickerBtn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:zPickerBtn];
    }

}
#pragma mark 确定选择picker
-(void)clickBtn{
    UIButton *btn = (UIButton *)[self.view viewWithTag:900];
    [btn setTitle:[zArrayData objectAtIndex:0] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _picker.hidden = YES;
    zPickerBtn.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
}
#pragma -mark UIPickerView 的代理
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [zArrayData count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [zArrayData objectAtIndex:row];
}

#pragma -mark 选中省市区的代理
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        zIndex = row;
}
#pragma mark - 返回
-(void)backGo{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
